// 本地存储工具类
export class Storage {
  // 设置存储
  static set(key: string, value: any): void {
    try {
      uni.setStorageSync(key, JSON.stringify(value))
    } catch (error) {
      console.error('Storage set error:', error)
    }
  }

  // 获取存储
  static get<T = any>(key: string, defaultValue?: T): T | null {
    try {
      const value = uni.getStorageSync(key)
      return value ? JSON.parse(value) : defaultValue || null
    } catch (error) {
      console.error('Storage get error:', error)
      return defaultValue || null
    }
  }

  // 删除存储
  static remove(key: string): void {
    try {
      uni.removeStorageSync(key)
    } catch (error) {
      console.error('Storage remove error:', error)
    }
  }

  // 清空存储
  static clear(): void {
    try {
      uni.clearStorageSync()
    } catch (error) {
      console.error('Storage clear error:', error)
    }
  }

  // 检查是否存在
  static has(key: string): boolean {
    try {
      const value = uni.getStorageSync(key)
      return value !== ''
    } catch (error) {
      return false
    }
  }
}

// 常用存储键名
export const STORAGE_KEYS = {
  TOKEN: 'token',
  REFRESH_TOKEN: 'refresh_token',
  USER_INFO: 'user_info',
  THEME: 'theme',
  LANGUAGE: 'language',
} as const
